package com.jinxin.phonecloudmusic.myapplication.model;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.phonecloudmusic.myapplication.app.JxscApp;
import com.jinxin.phonecloudmusic.myapplication.constant.NetConstant;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.BillboardList;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.Lyrics;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.Play;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.Search;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.Album;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.Artist;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.Billboard;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.Bitrate;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.Result;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.ResultList;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.SingerInformation;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.Song;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.SongInfo;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.SongList;
import com.jinxin.phonecloudmusic.myapplication.net.NetRequest;
import com.jinxin.phonecloudmusic.myapplication.net.NetUtil;
import com.jinxin.phonecloudmusic.myapplication.net.RequestValues;
import com.jinxin.phonecloudmusic.myapplication.utils.L;
import com.jinxin.phonecloudmusic.myapplication.utils.ToastUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZJ on 2017/9/27 0027.
 * 主页
 */

public class MainPageModel {
    //拿到榜单列表
    public BillboardList billboardList(final String method, final int type, final int size, final int offset){
        final NetRequest netRequest=new NetRequest() {
            @Override
            public JSONObject getServiceContent() {
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("method",method);
                jsonObject.put("type",type);
                jsonObject.put("size",size);
                jsonObject.put("offset",offset);
                return jsonObject;
            }
        };
        JSONObject response= NetUtil.getInstance().get(netRequest);
        BillboardList billboardList=new BillboardList();
        if (response != null && response.containsKey("serviceContent")) {
            JSONObject jsonObject= (JSONObject) response.get("serviceContent");
            //解析榜单
            JSONArray ja = jsonObject.getJSONArray("song_list");
            String a = ja.toString();
            List<SongList> songs = JSON.parseArray(ja.toString(), SongList.class);

            //解析广告单
            JSONObject jsBillboar = jsonObject.getJSONObject("billboard");
            String b = jsBillboar.toString();
            Billboard billboard = JSON.parseObject(jsBillboar.toString(), Billboard.class);

            //BillboardList billboardList = new BillboardList();
            billboardList.setLists(songs);
            billboardList.setBillboard(billboard);
        }
        return billboardList;
    }

    //播放
    public Play playMusic(final String method,final String songid){
        final NetRequest netRequest=new NetRequest() {
            @Override
            public JSONObject getServiceContent() {
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("method",method);
                jsonObject.put("songid", songid);
                return jsonObject;
            }
        };
        JSONObject response= NetUtil.getInstance().get(netRequest);
        Play play=new Play();
        if (null!=response&&response.containsKey("serviceContent")){
            JSONObject jsonObject= (JSONObject) response.get("serviceContent");
            //解析歌曲信息
            JSONObject JoSongInfo=jsonObject.getJSONObject("songinfo");
            SongInfo songInfo= JSON.parseObject(JoSongInfo.toString(),SongInfo.class);

            //解析比特率
            JSONObject JoBitrate=jsonObject.getJSONObject("bitrate");
            Bitrate bitrate=JSON.parseObject(JoBitrate.toString(), Bitrate.class);

            //Play play=new Play();
            play.setSongInfo(songInfo);
            play.setBitrate(bitrate);
        }
        return play;
    }

    //搜索
    public Search search(final String method,final String query){
        final NetRequest netRequest=new NetRequest() {
            @Override
            public JSONObject getServiceContent() {
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("method",method);
                try {
                    if (NetConstant.HTTP_ACCESSPATH.equals("http://192.168.60.102:8080/smarthome1/m-m/music3?")||NetConstant.HTTP_ACCESSPATH.equals("http://192.168.60.101:8080/smarthome1/m-m/music3?")) {
                        jsonObject.put("query", URLEncoder.encode(query, "utf-8"));
                    }//转换编码
                    else {
                        jsonObject.put("query", cnToUnicode(query));
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return jsonObject;
            }
        };
        JSONObject response= NetUtil.getInstance().get(netRequest);
        Search search=new Search();
        List<Album> albumList =new ArrayList<>();//根据歌名请求时没有返回这个属性
        List<Artist> artistList =new ArrayList<>();//时候歌曲歌名搜索没这个返回值
        List<Song> songList=new ArrayList<>();
        if (response!=null&&response.containsKey("serviceContent")){
            JSONObject jsonObject= (JSONObject) response.get("serviceContent");
            //解析歌曲
            JSONArray jsonSong = jsonObject.getJSONArray("song");
            L.e(response.toString() + "返回结果");
//            if (JSON.parseArray(jsonSong.toString(), Song.class).size()==0){
//
//            }
            //String a = jsonSong.toString();
            if (null!=jsonSong) {
                songList = JSON.parseArray(jsonSong.toString(), Song.class);
            }else {
                ToastUtil.showShort(JxscApp.getContext(),"暂无搜索结果");
            }


            //解析专辑
            if (response.containsKey("album")) {
                JSONArray jsonAlbum = jsonObject.getJSONArray("album");
                albumList = JSON.parseArray(jsonAlbum.toString(), Album.class);
            }

            //解析歌手
            if (response.containsKey("artist")) {
                JSONArray jsonArtist = jsonObject.getJSONArray("artist");
                artistList = JSON.parseArray(jsonArtist.toString(), Artist.class);
            }

            //Search search = new Search();
            search.setSongList(songList);
            if (jsonObject.containsKey("album")) {
                search.setAlbumList(albumList);
            }
            if (jsonObject.containsKey("artist")) {
                search.setArtistList(artistList);
            }
        }
        return search;
    }

    private static String cnToUnicode(String cn) {
        char[] chars = cn.toCharArray();
        String returnStr = "";
        for (int i = 0; i < chars.length; i++) {
            returnStr += "\\u" + Integer.toString(chars[i], 16);
        }
        Log.e("search+",returnStr);
        return returnStr;
    }

    //获取歌词
    public Lyrics lyrics(final String method,final String songid){
        NetRequest netRequest=new NetRequest() {
            @Override
            public JSONObject getServiceContent() {
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("method",method);
                jsonObject.put("songid", songid);
                return jsonObject;
            }
        };
        JSONObject response= NetUtil.getInstance().get(netRequest);
        Lyrics lyrics=new Lyrics();
        if (null!=response&&response.containsKey("serviceContent")){
            JSONObject jsonObject= (JSONObject) response.get("serviceContent");
            String title=jsonObject.getString("title");
            String lrcContent=jsonObject.getString("lrcContent");
            //Lyrics lyrics=new Lyrics();
            lyrics.setTitle(title);
            lyrics.setLrcContent(lrcContent);
        }
        return lyrics;
    }

    //获取歌手信息
    public SingerInformation singerInformation(final String method,final String tinguid){
        NetRequest netRequest=new NetRequest() {
            @Override
            public JSONObject getServiceContent() {
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("method",method);
                jsonObject.put("tinguid", tinguid);
                return jsonObject;
            }
        };
        JSONObject response= NetUtil.getInstance().get(netRequest);
        SingerInformation singerInformation=new SingerInformation();
        if (null!=response&&response.containsKey("serviceContent")){
            JSONObject jsonObject= (JSONObject) response.get("serviceContent");
            String intro = jsonObject.getString("intro");
            String name = jsonObject.getString("name");
            //SingerInformation singerInformation = new SingerInformation();
            singerInformation.setIntro(intro);
            singerInformation.setName(name);
        }
        return singerInformation;
    }

    //获取歌手歌曲列表
    public List<SongList> songLists(final String method, final String tinguid, final int limits){
        NetRequest netRequest=new NetRequest() {
            @Override
            public JSONObject getServiceContent() {
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("method", method);
                jsonObject.put("tinguid", tinguid);
                jsonObject.put("limits", limits);
                return jsonObject;
            }
        };
        JSONObject response= NetUtil.getInstance().get(netRequest);
        List<SongList> songList=new ArrayList<>();
        if (null!=response&&response.containsKey("serviceContent")){
            JSONObject jsonObject= (JSONObject) response.get("serviceContent");
            //解析歌手歌曲列表
            JSONArray jaSongList=jsonObject.getJSONArray("songlist");
            songList= JSON.parseArray(jaSongList.toString(),SongList.class);
        }
        return songList;
    }

    //获取推荐列表
    public List<ResultList> resultLists(final String method,final String song_id,final int num ){
        NetRequest netRequest=new NetRequest() {
            @Override
            public JSONObject getServiceContent() {
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("method", method);
                jsonObject.put("song_id", song_id);
                jsonObject.put("num", num);
                return jsonObject;
            }
        };
        JSONObject response= NetUtil.getInstance().get(netRequest);
        List<ResultList> resultLists=new ArrayList<>();
        if (null!=response&&response.containsKey("serviceContent")){
            JSONObject jsonObject= (JSONObject) response.get("serviceContent");
            JSONObject jsonObject1=jsonObject.getJSONObject("result");
            Result result= JSON.parseObject(jsonObject1.toString(), Result.class);
            JSONArray jsonArray=jsonObject1.getJSONArray("list");
            resultLists=JSON.parseArray(jsonArray.toString(),ResultList.class);
        }
        return resultLists;
    }
}
